package algorithm;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = new int[]{1,7,8,10,40,50,100,120};
        int i =binarySearch(arr,10);
        System.out.println(i);
    }
    public static int binarySearch(int[] arr,int num){
        int left = 0;
        int rifht=arr.length-1;
        int min;
        int i=0;
        while (left<=rifht){
            i++;
            min=(rifht+left)/2;
            if(arr[min]==num){
                System.out.println("执行："+i);
                return min;
            }else if(arr[min]>num){
                rifht=min-1;
            }else {
                left=min+1;
            }
        }
        return -1;
    }
}
